package com.itzw.hospital.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itzw.hospital.common.R;
import com.itzw.hospital.entity.User;
import com.itzw.hospital.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserMapper userMapper;

    /**
     * 用户注册
     * @param user
     * @return
     */
    @PostMapping
    public R<String> register(@RequestBody User user){
        user.setIdentity("用户");
        userMapper.insert(user);
        return R.success("注册成功");
    }

    @PostMapping("/login")
    public R<User> login(@RequestBody User user, HttpSession session){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,user.getUsername());
        User u = userMapper.selectOne(queryWrapper);

        if (u==null){
            return R.error("登录失败");
        }

        if (!u.getPassword().equals(user.getPassword())){
            return R.error("登录失败");
        }

        session.setAttribute("user",user.getId());
        return R.success(u);
    }

    @GetMapping("/info")
    public R<List<User>> info(){
        List<User> userList = userMapper.selectList(null);
        return R.success(userList);
    }















}
